import { FPSCountercontroller } from "./FPSCounter";
import { PlayerAction, PrintComponent } from "./PrintComponent";

/** 渠道 */
enum TChannel {
    applovin = 'applovin',
    facebook = 'facebook',
    google = 'google',
    ironsource = 'ironsource',
    mintegral = 'mintegral',
    moloco = 'moloco',
    tiktok = 'tiktok',
    unity = 'unity',
}

/**
 * super-html playable adapter
 * @help https://store.cocos.com/app/detail/3657
 * @home https://github.com/magician-f/cocos-playable-demo
 * @author https://github.com/magician-f
 */
export default class SHtml {

    private static _instance: SHtml;
    public static get instance(): SHtml {
        if (!this._instance) {
            this._instance = new SHtml();
        }
        return this._instance;
    };

    getHtml() {
        return window['super_html'];
    }

    getChannel() {
        let channel = window['super_html_channel'];
        console.log('channel: ', channel);
        return channel ? channel : TChannel.applovin;
    }

    getStoreUrl() {
        let isAndroid = this.is_android();
        if (isAndroid) {
            let html = this.getHtml();
            if (html) {
                return html.google_play_url;
            }
        }
        else {
            let html = this.getHtml();
            if (html) {
                return html.appstore_url;
            }
        }
        return '';
    }

    private touchNumber = 0;
    private touchFinish = false;
    private FPSController: FPSCountercontroller;

    gameReady() {
        let html = this.getHtml();
        if (html && html.game_ready) {
            html.game_ready();
        }
    }

    gameEnd() {
        let html = this.getHtml();
        if (html && html.game_end) {
            html.game_end();
        }
    }

    download() {
        let html = this.getHtml();
        if (html && html.download) {
            html.download(this.getStoreUrl());
        }
    }

    /**
     * 是否隐藏下载按钮，意味着使用平台注入的下载按钮
     * channel : google
     */
    is_hide_download() {
        let html = this.getHtml();
        if (html && html.is_hide_download) {
            return html.is_hide_download();
        }
        return false;
    }

    check_auto_download() {
        let channel = this.getChannel();
        if (channel == TChannel.applovin || channel == TChannel.moloco) {
            return true;
        }
        return false;
    }

    check_print() {
        let channel = this.getChannel();
        if (channel == TChannel.applovin || channel == TChannel.moloco) {
            return true;
        }
        return false;
    }

    /** 检测是否是android渠道 */
    is_android() {
        return this.detect_os() === "Android";
    }

    /** 获取设备系统 */
    detect_os() {
        //@ts-ignore
        if (navigator.userAgentData && navigator.userAgentData.platform) {
            //@ts-ignore
            const platform = navigator.userAgentData.platform;
            return platform;
        }
        const userAgent = navigator.userAgent || "";
        if (/android/i.test(userAgent)) {
            return "Android";
        }
        return "Unknown OS";
    }

    /************************************************************************************************/
    /********************************************* 打点 *********************************************/
    /************************************************************************************************/
    isPrint = false;
    /** 初始化 */
    pInit() {
        if (this.check_print()) {
            this.isPrint = true;
            PrintComponent.init();
            PrintComponent.setMediaAndMaterial(this.getChannel(), 1);
            this.FPSController = new FPSCountercontroller();
            this.FPSController.start();
        }
    }

    /** loading结束调用 */
    pLoading() {
        if (!this.isPrint) {
            return;
        }
        PrintComponent.loading();
        console.log('Print loading');
    }

    /** 游戏开始时调用 */
    pGameStart() {
        if (!this.isPrint) {
            return;
        }
        PrintComponent.startGame();
        console.log('Print startGame');
    }

    /** 游戏结束时调用 */
    pGameFinish(success: boolean) {
        if (!this.isPrint) {
            return;
        }
        PrintComponent.endGame(success);
        console.log('Print endGame success: ', success);
    }

    /** 点击重新开始时调用 */
    pReplay() {
        if (!this.isPrint) {
            return;
        }
        PrintComponent.replay();
        console.log('Print replay');
    }

    /** 操作主角时调用 */
    pInteraction(type: string) {
        if (!this.isPrint) {
            return;
        }
        if (this.touchFinish) {
            return;
        }

        this.touchNumber++;
        PrintComponent.game_interaction(type);
        console.log('Print game_interaction type: ', type);
    }

    /** 游戏完结后，touch 的 start 和 end 成对出现 */
    pTouchFinish() {
        if (this.touchNumber % 2 != 0) {
            this.pInteraction('touch-end');
            this.touchFinish = true;
        }
    }

    /** 跳转到商店调用 */
    pActionbar(action: PlayerAction) {
        if (!this.isPrint) {
            return;
        }
        PrintComponent.actionbar(action);
        console.log('Print actionbar action: ', action);
    }

    /** 在完成加载后每帧循环调用卡顿检测 */
    pStuck(dt: number, stage: number = 1, force: boolean = false) {
        if (!this.isPrint) {
            return;
        }
        this.FPSController.stutter(dt, stage, force);
    }
    /************************************************************************************************/
    /********************************************** end *********************************************/
    /************************************************************************************************/
}